package com.juyu.miliao.scheduler.dao.mysql;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.juyu.miliao.scheduler.domin.BillDetail;

import javax.annotation.Resource;

@Repository
public class BillDetailDao {
	@Resource
	private SqlSession sqlSession;

	public BillDetail findDiamondSumByDay(long userId, String threeWeeks, String tomorrow) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("day", threeWeeks);
		params.put("tomorrow", tomorrow);
		return sqlSession.selectOne("findDiamondSumByDay", params);
	}

	public BillDetail findSpendSumByDay(long userId, String threeWeeks, String tomorrow) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("day", threeWeeks);
		params.put("tomorrow", tomorrow);
		return sqlSession.selectOne("findSpendSumByDay", params);
	}

	/**
	 * @param userId
	 * @return
	 */
	public int getLastWeekDiamondByUserId(long userId) {
		// 获取今天是一个星期的第几天
		int value = LocalDate.now().getDayOfWeek().getValue();
		// 获取上周一 00:00:01时间
		LocalDateTime startDays = LocalDate.now().minusWeeks(1).minusDays(value - 1).atTime(0, 0, 1);
		// 获取上周日 23:59:59 时间
		LocalDateTime endDays = LocalDate.now().minusWeeks(1).plusDays(7 - value).atTime(23, 59, 59);
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("startDays", startDays);
		params.put("endDays", endDays);
		return sqlSession.selectOne("getLastWeekDiamondByUserId", params);
	}

	/**
	 * @param userId
	 * @return
	 */
	public int getDiamondRecord(long userId) {
		// 获取今天是一个星期的第几天
		int value = LocalDate.now().getDayOfWeek().getValue();
		// 获取上周一 00:00:01时间
		LocalDateTime startDays = LocalDate.now().minusWeeks(1).minusDays(value - 1).atTime(0, 0, 1);
		// 获取上周日 23:59:59 时间
		LocalDateTime endDays = LocalDate.now().minusWeeks(1).plusDays(7 - value).atTime(23, 59, 59);
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		params.put("startDays", startDays);
		params.put("endDays", endDays);
		return sqlSession.selectOne("getDiamondRecord", params);
	}
}
